package com.nstar.base.preloader
{
    import flash.display.BitmapData;
    import flash.display.GradientType;
    import flash.display.Graphics;
    import flash.display.Loader;
    import flash.display.SpreadMethod;
    import flash.display.Sprite;
    import flash.geom.Matrix;
    
    import mx.graphics.codec.PNGEncoder;   
       
    public class WelcomeScreen extends Loader   
    {   
        private static const LOGO_WIDTH:int = 238;
        private static const LOGO_HEIGHT:int = 87;
        
        private static const BAR_WIDTH:int = LOGO_WIDTH - 30;   
        private static const BAR_HEIGHT:int = 10;   
        private static const BAR_INNER_COLOR:uint = 0x34b5f9;   
        private static const START_COLOR:uint = 0x346bac;
        private static const END_COLOR :uint = 0x34b5f9;
           
        private var isReady:Boolean = false;   
        public  var progress:Number;   
        private var progressBMP : BitmapData;   
           
        public function WelcomeScreen()   
        {   
        }   
           
        override public function get width() : Number   
        {   
            return BAR_WIDTH;   
        }   
           
        override public function get height() : Number   
        {   
            return BAR_HEIGHT;   
        }   
        /**  
        * 进度载入完毕后，隐藏进度条  
        */  
        public function set ready(value : Boolean) : void  
        {   
            this.isReady = value;   
            this.visible = !this.isReady;   
        }   
           
        public function get ready() : Boolean { return this.isReady; }   
        /**  
        * 刷新函数，每个时钟周期都被调用的函数  
        */  
        public function refresh():void  
        {   
            progressBMP = this.drawProgressBar();   
            var encoder:PNGEncoder = new PNGEncoder();   
            this.loadBytes(encoder.encode(progressBMP));   
        }   
        /**  
        * 进度条生成函数  
        */  
        private function drawProgressBar():BitmapData   
        {   
            // create bitmap data to create the data   
            var bmp : BitmapData = new BitmapData(LOGO_WIDTH, LOGO_HEIGHT, true, 0);   
            // draw the progress bar   
            var s:Sprite = new Sprite();   
            var g:Graphics = s.graphics;   
            // draw the bar background   
            
            var px : int = (LOGO_WIDTH -BAR_WIDTH)  / 2;   
            var py : int = (LOGO_HEIGHT - BAR_HEIGHT) / 2;
            var progressWidth:Number = BAR_WIDTH * this.progress / 100;   
            
            //画填充
            g.lineStyle(1, BAR_INNER_COLOR,1);   
			
            var matr:Matrix = new Matrix();
  			matr.createGradientBox(progressWidth, BAR_HEIGHT);
  			var spreadMethod:String = SpreadMethod.PAD;
            g.beginGradientFill( GradientType.LINEAR,[START_COLOR,END_COLOR],[1,1],[0,255],matr,spreadMethod);

            //根据progress进度，来画出进度条的长度   
            g.drawRect(px, py, progressWidth, BAR_HEIGHT);   
            bmp.draw(s);   
            return bmp;   
        }   
        
    }
}